package app.sceledroid.activity.course.forum.DiscussionActivity;

import java.util.ArrayList;
import app.sceledroid.entity.forum.*;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import app.sceledroid.R;
import app.sceledroid.activity.MainActivity;
import app.sceledroid.activity.course.ResourceActivity;
import app.sceledroid.adapter.DeadlineAdapter;
import app.sceledroid.adapter.DiscussionAdapter;
import app.sceledroid.adapter.ResourceAdapter;
import app.sceledroid.entity.Deadline;
import app.sceledroid.entity.Resource;
import app.sceledroid.network.DownloadHandler;
import app.sceledroid.services.SceleService;
import app.sceledroid.services.SceleService.SceleBinder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import app.sceledroid.R;
import app.sceledroid.activity.DirectEmailActivity;
import app.sceledroid.activity.LoginActivity;
import app.sceledroid.activity.SettingsActivity;
import app.sceledroid.adapter.DiscussionAdapter;
import app.sceledroid.entity.Profile;
import app.sceledroid.network.SearchHandler;
import app.sceledroid.services.SceleService;

public class DiscussionActivity extends Activity {
	
	private SceleService sceleService;
	private boolean isBounded;
	private ArrayList<Discussion> discussions;
	private DiscussionAdapter discussionAdapter;
	private String forumName;
	
	private ServiceConnection sceleServiceConnection = new ServiceConnection() {

		//@Override
		public void onServiceConnected(ComponentName componentName,
				IBinder iBinder) {
			// TODO Auto-generated method stub
			SceleBinder binder = (SceleBinder) iBinder;
			sceleService = binder.getService();
			isBounded = true;
		}

		//@Override
		public void onServiceDisconnected(ComponentName componentName) {
			// TODO Auto-generated method stub
			isBounded = false;
		}

	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forum_discussion_view);
		
		discussions = new ArrayList<Discussion>();
		
		discussionAdapter = new DiscussionAdapter(this,
				R.id.discussion_list_no_item, discussions);
		
		Intent service = new Intent(this, SceleService.class);
		startService(service);
		
		Bundle bundle = getIntent().getExtras();
		forumName = bundle.getString("ForumName");
		
		final Button fetchButton = (Button) findViewById(R.id.fetch_discussion);
		ListView discussionListView = (ListView) findViewById(R.id.discussionList);
		
		discussionListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Discussion r = discussionAdapter.getItem(arg2);
				Toast.makeText(DiscussionActivity.this, r.getTopic()+ r.getUrl(),
						Toast.LENGTH_LONG);
				//sementara manggil browser activity buat handle view isi discussion
				Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(r.getUrl()));
				ComponentName cn = new ComponentName("com.android.browser", 
				"com.android.browser.BrowserActivity");
				
				browse.setComponent(cn);
				startActivity(browse);
			}
			
		});
		
		discussionListView.setAdapter(discussionAdapter);
		
		fetchButton.setOnClickListener( new OnClickListener() {
			
			//@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if (sceleService != null) {
					Toast.makeText(DiscussionActivity.this,
							"Scele Service Not NULL", Toast.LENGTH_LONG).show();
					
					ArrayList<Discussion> temp = sceleService.getDiscussion(forumName);
					Log.v("Forum Name : ", forumName );
					
					if (temp != null) {
						discussions.addAll(temp);
						for (Discussion r : discussions) {
							Log.v("Discussion : ", r.getTopic() + r.getUrl());
						}
						discussionAdapter.notifyDataSetChanged();

						fetchButton.setVisibility(View.GONE);
					}
					else
					{
						Toast.makeText(DiscussionActivity.this,
								"Discussions is NULL", Toast.LENGTH_LONG).show();
					}
					
				} 
				else {	
					Toast.makeText(DiscussionActivity.this, "Scele Service NULL",
							Toast.LENGTH_LONG).show();
					bindToService();
				}
		
			}
		});
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		bindToService();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	private void bindToService() {
		Intent service = new Intent(this, SceleService.class);
		bindService(service, sceleServiceConnection, Context.BIND_AUTO_CREATE);

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.afterlogin_menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//intent for settings
		Intent intent  = new Intent(DiscussionActivity.this, SettingsActivity.class);
		//toast for others
		Context context = getApplicationContext();
		CharSequence help = "SceleDroid";
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(context, help, duration);
		
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.help:
	       // showHelp();
	    	toast.show();
	        return true;
	    case R.id.settings:
	    	startActivity(intent);
	    	return true;
	    case R.id.credits:
	    	toast.show();
	    	//credits here;
	    	return true;
	    case R.id.logout:
	    	intent = new Intent(DiscussionActivity.this, LoginActivity.class);
	    	startActivity(intent);
	    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
			Editor e = pref.edit();//this.getPreferences(Context.MODE_PRIVATE).edit();
			e.clear();
			e.commit();
	    	return true;
	    case R.id.search:
	    	intent = new Intent(DiscussionActivity.this, DiscussionActivity.class);
	    	startActivity(intent);
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
